/*
 * DesignAssignment2.c
 *
 * Created: 3/9/2014 9:52:34 PM
 *  Author: tawataob
 */ 


#include <avr/io.h>
#include "avr/interrupt.h"
#define F_CPU 1000000UL //fclk = 1MHz
#include <util/delay.h>

volatile int catch2nd = 0; // flag to indicate catching every 2nd interrupt

ISR(INT0_vect)
{
	cli(); //disable interrupts
	if(catch2nd == 1)
	{
		catch2nd = 0; // reset 2nd interrupt flag
		PORTC ^= 0x02; // 2nd interrupt caught, toggle 10th LED
	}
	else
	{
		catch2nd = 1; // 1st interrupt caught, set flag for next interrupt
	}
	sei(); // re-enable interrupts
}


int main(void)
{
	int count1 = 0; //counter for 8-bit LED bar
	int count2 = 0; //counter for 10 falling edges
	DDRB |= 0xFF; //set all of port B to output
	DDRC |= 0x03; //set PC0 and PC1 to be outputs for 9th and 10th LEDs respectively
	PORTB = count1; // set the 8 LEDs to off
	PORTC = 0; // set PC0 and PC1 to off
	EICRA |= 0x02; //set the interrupt to occur on falling edge
	EIMSK |= (1<<INT0);
	sei(); //enable interrupts
	
	while(1)
	{
		_delay_ms(250); //rising edge of 50% duty-cycle 0.5sec period waveform
		count1++; //updating the counters
		count2++;
		PORTB = count1; //outputting to the LED bar
		if(count2==10)
		{
			count2 = 0; //resetting the 10 edge counter
			PORTC ^= 0x01; //toggling the 9th bit on the LED bar
		}
		_delay_ms(250); //falling edge of waveform
	}
	
}